package org.thunms.framework.controller;

import java.io.Serializable;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.thunms.framework.utils.CustomBooleanEditor;
import org.thunms.framework.utils.CustomDateTypeAutoEditor;
import org.thunms.framework.utils.CustomStringTrimEditor;
public class ControllerDefaultBase   implements Serializable {
	private static final long serialVersionUID = 1430921385927797651L;
	private static final Logger logger = LoggerFactory.getLogger(ControllerDefaultBase.class);
	protected String platformId;
	protected String systemId;
	protected String moduleId;
	protected String initpage;
	protected String basepage;
	

	public ControllerDefaultBase(String platformId, String systemId, String moduleId) {
		super();
		this.platformId = platformId;
		this.systemId = systemId;
		this.moduleId = moduleId;
		this.initpage=new StringBuffer(this.platformId).append("/")
				.append(this.systemId).append("/").append(this.moduleId)
				.append("/").append(moduleId).toString();
		this.basepage=new StringBuffer(this.platformId).append("/")
				.append(this.systemId).append("/").append(this.moduleId)
				.append("/").toString();
	}

	/**
	 * 将前台传递过来的日期格式的字符串，自动转化为Date类型
	 * 
	 * @param binder
	 * 
	 */
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		logger.info("开始格式化日期");
		binder.registerCustomEditor(Date.class, new CustomDateTypeAutoEditor());
		logger.info("结束格式化日期");
		logger.debug("格式化Boolean");
		binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor());
		binder.registerCustomEditor(String.class, new CustomStringTrimEditor());
		initBinderCallBack(binder);
	}
	/**
	 * 对象注解格式化规则回调扩充方法
	 * @param binder
	 */
	protected void initBinderCallBack(ServletRequestDataBinder binder) {
		
	}
		/**
		 * 页面路径样式 platformId/systemId/moduleId/moduleId_method
		 * @param method
		 * @return
		 */
	//@Cacheable(value="thunmsControllerCache")
		protected String getRerutnURL(String method) {
			return new StringBuffer(initpage).append("_").append(method).toString();

		}
		/**
		 * 重定向页面
		 * @param method
		 * @return
		 */
		protected String getRedirectURL(String method) {
			return new StringBuffer("redirect:/").append(this.basepage).append(method).toString();

		}
		
		/**
		 * 页面路径样式 platformId/systemId/moduleId/pageName
		 * @param method
		 * @return
		 */
		protected String getRerutnURLBybase(String pageName) {
			return new StringBuffer(this.basepage).append(pageName).toString();

		}
		//@Cacheable(value="thunmsControllerCache")
		protected String getCacheKey(){
			 return new StringBuffer("org.thunms.").append(this.platformId).append(".")
					.append(this.systemId).append(".").append(this.moduleId)
					.toString();
			
		}
		
}
